SlideShare a Scribd company logo
Class No.07  Data Structures http://ecomputernotes.com
Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ http://ecomputernotes.com
Infix to Postfix Note that the postfix form an expression does not require parenthesis. Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second. The postfix forms are: 4+3*5 435*+  (4+3)*5 43+5* http://ecomputernotes.com
Evaluating Postfix  Each operator in a postfix expression refers to the previous two operands. Each time we read an operand, we push it on a stack. When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack.  http://ecomputernotes.com
Evaluating Postfix Stack s; while( not end of input ) { e = get next element of input if( e is an operand ) s.push( e ); else { op2 = s.pop(); op1 = s.pop(); value = result of applying operator ‘e’ to op1 and op2; s.push( value ); } } finalresult = s.pop(); http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 3 7 2 49 49,3 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
Converting Infix to Postfix Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’.  The postfix versions are ‘ABC*+’ and ‘AB+C*’. The order of operands in postfix is the same as the infix. In scanning from left to right, the operand ‘A’ can be inserted into postfix expression. http://ecomputernotes.com
Converting Infix to Postfix The ‘+’ cannot be inserted until its second operand has been scanned and inserted. The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence. http://ecomputernotes.com
Converting Infix to Postfix In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first. Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators. Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise.
Converting Infix to Postfix prcd(‘*’,’+’) is TRUE prcd(‘+’,’+’) is TRUE prcd(‘+’,’*’) is FALSE  Here is the algorithm that converts infix expression to its postfix form. The infix expression is without parenthesis.
Converting Infix to Postfix Stack s; While( not end of input ) { c = next input character;  if( c is an operand ) add c to postfix string; else { while( !s.empty() && prcd(s.top(),c) ){ op = s.pop();  add op to the postfix string; } s.push( c ); } while( !s.empty() ) { op = s.pop(); add op to postfix string; }
Converting Infix to Postfix Example: A + B * C symb postfix stack A A
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A +
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB +
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + *
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + *
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * +
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * +
Converting Infix to Postfix Handling parenthesis When an open parenthesis ‘(‘ is read, it must be pushed on the stack. This can be done by setting prcd(op,‘(‘ ) to be FALSE.  Also, prcd( ‘(‘,op ) == FALSE which ensures that an operator after ‘(‘ is pushed on the stack.
Converting Infix to Postfix When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and placed in the postfix string. To do this, prcd( op,’)’ ) == TRUE. Both the ‘(‘ and the ‘)’ must be discarded: prcd( ‘(‘,’)’ ) == FALSE. Need to change line 11 of the algorithm.
Converting Infix to Postfix if( s.empty() || symb != ‘)’ )  s.push( c ); else s.pop(); // discard the ‘(‘  prcd( ‘(‘, op ) = FALSE for any operator prcd( op, ‘)’ ) = FALSE for any operator other than ‘)’ prcd( op, ‘)’ ) = TRUE for any operator  other than ‘(‘ prcd( ‘)’, op ) = error for any operator.

More Related Content

PPT
Computer notes - Postfix
PDF
Ds stack 03
PPTX
Prefix, Infix and Post-fix Notations
PPTX
My lecture infix-to-postfix
PPT
Application of Stacks
PPT
Expression evaluation
PPT
Circular queues
PPT
Computer notes - Postfix
Ds stack 03
Prefix, Infix and Post-fix Notations
My lecture infix-to-postfix
Application of Stacks
Expression evaluation
Circular queues

What's hot (18)

PPT
Infix prefix postfix
DOC
Infix to-postfix examples
PPT
Conversion of Infix To Postfix Expressions
PPTX
Scope and closures
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
PPTX
Data structures
PPTX
Stack and queue -polish notations
PPT
Infix to Postfix Conversion Using Stack
PDF
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
PPTX
Application of Stack - Yadraj Meena
PPTX
Infix postfixcoversion
PPTX
Polish
PPTX
Nested loops
PDF
Reliability Patterns for Fun and Profit
PPT
Computer notes data structures - 9
PPT
Whats new in_csharp4
DOC
35787646 system-software-lab-manual
PDF
C言語静的解析ツールと Ruby 1.9 trunk
Infix prefix postfix
Infix to-postfix examples
Conversion of Infix To Postfix Expressions
Scope and closures
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
Data structures
Stack and queue -polish notations
Infix to Postfix Conversion Using Stack
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Application of Stack - Yadraj Meena
Infix postfixcoversion
Polish
Nested loops
Reliability Patterns for Fun and Profit
Computer notes data structures - 9
Whats new in_csharp4
35787646 system-software-lab-manual
C言語静的解析ツールと Ruby 1.9 trunk
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 16
computer notes - Data Structures - 32
computer notes - Data Structures - 4
computer notes - Data Structures - 22
computer notes - Data Structures - 26
computer notes - Data Structures - 29
computer notes - Data Structures - 1
computer notes - Data Structures - 18
computer notes - Data Structures - 33
computer notes - Data Structures - 39
computer notes - Data Structures - 19
computer notes - Data Structures - 38
computer notes - Data Structures - 35
computer notes - Data Structures - 9
computer notes - Data Structures - 8
computer notes - Data Structures - 11
computer notes - Data Structures - 5
computer notes - Data Structures - 15
computer notes - Data Structures - 31
computer notes - Data Structures - 21
computer notes - Data Structures - 16
Ad

Similar to computer notes - Data Structures - 7 (20)

PPT
data structure and algorithm by bomboat_3
PDF
Applications of Stack
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
PPTX
Topic 2_revised.pptx
PPTX
Stack_Application_Infix_Prefix.pptx
PPTX
week9-prefixinfixandpostfixnotations-191013065821.pptx
PDF
Java Bytecodes by Example
PDF
Lab Manual IV (1).pdf on C++ Programming practice
PDF
Applications of stack
PPTX
Programming Homework Help
PDF
The concept of stack is extremely important in computer science and .pdf
PDF
1.3- infix-ti-postfix.pdf
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
PDF
ECMAScript 6
PDF
Data structure and algorithm.(dsa)
PPTX
Lect-5 & 6.pptx
PPT
About Go
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
data structure and algorithm by bomboat_3
Applications of Stack
Data Structure and Algorithms by Sabeen Memon03.pptx
Topic 2_revised.pptx
Stack_Application_Infix_Prefix.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
Java Bytecodes by Example
Lab Manual IV (1).pdf on C++ Programming practice
Applications of stack
Programming Homework Help
The concept of stack is extremely important in computer science and .pdf
1.3- infix-ti-postfix.pdf
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
ECMAScript 6
Data structure and algorithm.(dsa)
Lect-5 & 6.pptx
About Go
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual

More from ecomputernotes (18)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
computer notes - Data Structures - 10
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Empathic Computing: Creating Shared Understanding
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Programs and apps: productivity, graphics, security and other tools
Agricultural_Statistics_at_a_Glance_2022_0.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Empathic Computing: Creating Shared Understanding
The AUB Centre for AI in Media Proposal.docx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MIND Revenue Release Quarter 2 2025 Press Release
Review of recent advances in non-invasive hemoglobin estimation
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Unlocking AI with Model Context Protocol (MCP)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

computer notes - Data Structures - 7

  • 1. Class No.07 Data Structures http://ecomputernotes.com
  • 2. Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ http://ecomputernotes.com
  • 3. Infix to Postfix Note that the postfix form an expression does not require parenthesis. Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second. The postfix forms are: 4+3*5 435*+ (4+3)*5 43+5* http://ecomputernotes.com
  • 4. Evaluating Postfix Each operator in a postfix expression refers to the previous two operands. Each time we read an operand, we push it on a stack. When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack. http://ecomputernotes.com
  • 5. Evaluating Postfix Stack s; while( not end of input ) { e = get next element of input if( e is an operand ) s.push( e ); else { op2 = s.pop(); op1 = s.pop(); value = result of applying operator ‘e’ to op1 and op2; s.push( value ); } } finalresult = s.pop(); http://ecomputernotes.com
  • 6. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 http://ecomputernotes.com
  • 7. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 http://ecomputernotes.com
  • 8. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 http://ecomputernotes.com
  • 9. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 http://ecomputernotes.com
  • 10. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 http://ecomputernotes.com
  • 11. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 http://ecomputernotes.com
  • 12. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 http://ecomputernotes.com
  • 13. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 http://ecomputernotes.com
  • 14. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 http://ecomputernotes.com
  • 15. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 http://ecomputernotes.com
  • 16. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 http://ecomputernotes.com
  • 17. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2 http://ecomputernotes.com
  • 18. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 http://ecomputernotes.com
  • 19. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 http://ecomputernotes.com
  • 20. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
  • 21. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
  • 22. Converting Infix to Postfix Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’. The postfix versions are ‘ABC*+’ and ‘AB+C*’. The order of operands in postfix is the same as the infix. In scanning from left to right, the operand ‘A’ can be inserted into postfix expression. http://ecomputernotes.com
  • 23. Converting Infix to Postfix The ‘+’ cannot be inserted until its second operand has been scanned and inserted. The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence. http://ecomputernotes.com
  • 24. Converting Infix to Postfix In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first. Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators. Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise.
  • 25. Converting Infix to Postfix prcd(‘*’,’+’) is TRUE prcd(‘+’,’+’) is TRUE prcd(‘+’,’*’) is FALSE Here is the algorithm that converts infix expression to its postfix form. The infix expression is without parenthesis.
  • 26. Converting Infix to Postfix Stack s; While( not end of input ) { c = next input character; if( c is an operand ) add c to postfix string; else { while( !s.empty() && prcd(s.top(),c) ){ op = s.pop(); add op to the postfix string; } s.push( c ); } while( !s.empty() ) { op = s.pop(); add op to postfix string; }
  • 27. Converting Infix to Postfix Example: A + B * C symb postfix stack A A
  • 28. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A +
  • 29. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB +
  • 30. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + *
  • 31. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + *
  • 32. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * +
  • 33. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * +
  • 34. Converting Infix to Postfix Handling parenthesis When an open parenthesis ‘(‘ is read, it must be pushed on the stack. This can be done by setting prcd(op,‘(‘ ) to be FALSE. Also, prcd( ‘(‘,op ) == FALSE which ensures that an operator after ‘(‘ is pushed on the stack.
  • 35. Converting Infix to Postfix When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and placed in the postfix string. To do this, prcd( op,’)’ ) == TRUE. Both the ‘(‘ and the ‘)’ must be discarded: prcd( ‘(‘,’)’ ) == FALSE. Need to change line 11 of the algorithm.
  • 36. Converting Infix to Postfix if( s.empty() || symb != ‘)’ ) s.push( c ); else s.pop(); // discard the ‘(‘ prcd( ‘(‘, op ) = FALSE for any operator prcd( op, ‘)’ ) = FALSE for any operator other than ‘)’ prcd( op, ‘)’ ) = TRUE for any operator other than ‘(‘ prcd( ‘)’, op ) = error for any operator.

Editor's Notes

  • #3: End of lecture 6
  • #4: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #5: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #6: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #7: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #8: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #9: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #10: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #11: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #12: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #13: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #14: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #15: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #16: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #17: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #18: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #19: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #20: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #21: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #22: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #23: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #24: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #25: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #26: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #27: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #28: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #29: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #30: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #31: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #32: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #33: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #34: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #35: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #36: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #37: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.